package com.android.media;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.JsonReader;
import android.util.JsonToken;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.baselib.utils.LogUtils;
import com.media.cache.CacheManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PlayFeatureActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private EditText mVideoUrlView;
    private Button mPlayBtn;
    private ListView mVideoListView;

    private RadioGroup mPlayerBtnGroup;
    private RadioButton mIjkPlayerBtn;
    private RadioButton mExoPlayerBtn;

    private CheckBox mVideoCacheBox;
    private TextView mCachedLocationView;
    private TextView mCacheSizeView;
    private TextView mClearCacheView;

    private List<HashMap<String, String>> mVideoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_func);

        initViews();
        initViewListData();
    }

    private void initViews() {
        mVideoUrlView = (EditText) findViewById(R.id.video_url_view);
        mPlayBtn = (Button) findViewById(R.id.play_btn);
        mVideoListView = (ListView) findViewById(R.id.video_list);
        mPlayerBtnGroup = (RadioGroup) findViewById(R.id.play_btn_group);
        mIjkPlayerBtn = (RadioButton) findViewById(R.id.ijkplayer_btn);
        mExoPlayerBtn = (RadioButton) findViewById(R.id.exoplayer_btn);
        mVideoCacheBox = (CheckBox) findViewById(R.id.local_proxy_box);
        mCachedLocationView = (TextView) findViewById(R.id.cached_location_view);
        mCacheSizeView = (TextView) findViewById(R.id.cache_size_view);
        mClearCacheView = (TextView) findViewById(R.id.clear_cache_view);

        mExoPlayerBtn.setChecked(true);

        mPlayBtn.setOnClickListener(this);
        mClearCacheView.setOnClickListener(this);

        mPlayerBtnGroup.setOnCheckedChangeListener(this);

        mCachedLocationView.setText(CacheManager.getCachePath());
        String url = "";
//        url = "http://hvod.tv189.cn/tyzz/vod/test/test.mp4";
//        url = "http://hvod.tv189.cn/tyzz/vod/test/test.mp4";
//            url = "http://192.168.2.1/hls/enc2.m3u8";//未加密
//            url = "http://192.168.2.1/vod/mov_enc.mp4";
//            url = "http://192.168.188.13/hls/enc.m3u8";
//        url = "https://tp.nty.tv189.com/tss/C44368739/C44368739.m3u8";//一舟加密地址
        url = "https://tp.nty.tv189.com/tss/C44368423/C44368423.m3u8";//一舟加密地址（红毯先生）
//        url = "http://vod3.nty.tv189.cn/6/ol/st02/2024/08/16/Q600_d3bb3342-829f-4616-adbc-def2967d434e.mp4.m3u8?sign=4e87784508d9813a9f184052618b1bf2&qualityCode=796&qualityid=16&version=1&guid=f4dede13-fc18-752b-930e-d8785e63b27b&app=115010310149&cookie=cc5cf4ba142f54fd8855dbfba446d269&session=cc5cf4ba142f54fd8855dbfba446d269&uid=0&uname=MTAwMDAwMDAwMDA%3D&time=20240822102908&videotype=2&cid=C44368423&cname=&cateid=&dev=000001&ep=1&os=30&ps=0099&clienttype=&appver=&res=&channelid=&pid=1000000129&orderid=&nid=&cp=20240180&sp=00000014&ip=MTcyLjI0LjEuNjY%3D&ipSign=89d8ff05de55dc5c8fd2ff0c580d8d51&cdntoken=api_66c6a2743121f&a=qCa%2FcbmIX44%3D&pvv=&t=66c6dab4&cf=tx&s2=cfcd208495d565ef66e7dff9f98764da";
        mVideoUrlView.setText(url);
    }

    private void initViewListData() {
        mVideoList = new ArrayList<>();
        SimpleAdapter adapter = new SimpleAdapter(this, mVideoList, R.layout.video_item, new String[]{"name"}, new int[]{R.id.video_name});
        mVideoListView.setAdapter(adapter);
        mVideoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = mVideoList.get(position).get("url");
                mVideoUrlView.setText(url);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        mCacheSizeView.setText(CacheManager.getCachedSize());
    }

    @Override
    public void onClick(View v) {
        if (v == mPlayBtn) {
            doPlayVideo();
        } else if (v == mClearCacheView) {
            clearVideoCache();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        LogUtils.d("onCheckedChanged checkedId = " + checkedId);
    }

    private void doPlayVideo() {
        String url = mVideoUrlView.getText().toString();
        if (TextUtils.isEmpty(url)) {
            Toast.makeText(this, "输入的url为空", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(this, PlayerActivity.class);
            intent.putExtra("url", url);

            int playerType = -1;
            if (mIjkPlayerBtn.isChecked()) {
                playerType = 1;
            } else if (mExoPlayerBtn.isChecked()) {
                playerType = 2;
            }
            intent.putExtra("playerType", playerType);
            boolean videoCached = mVideoCacheBox.isChecked();
            intent.putExtra("videoCached", videoCached);

            startActivity(intent);
        }
    }

    private void clearVideoCache() {
        CacheManager.deleteCacheFile();
        mCacheSizeView.setText("0 MB");
    }
}
